package com.yidu.ladder.system.controller;

import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.yidu.ladder.base.domain.Role;
import com.yidu.ladder.base.domain.RoleVo;
import com.yidu.ladder.system.domain.Resource;
import com.yidu.ladder.system.domain.User;
import com.yidu.ladder.system.domain.UserVo;
import com.yidu.ladder.system.service.PowerService;


/**
 *      ┌─┐       ┌─┐ + +
 *   ┌──┘ ┴───────┘ ┴──┐++
 *   │                 │
 *   │       ───       │++ + + +
 *   ███████───███████ │+
 *   │                 │+
 *   │       ─┴─       │
 *   │                 │
 *   └───┐         ┌───┘
 *       │         │
 *       │         │   + +
 *       │         │
 *       │         └──────────────┐
 *       │                        │
 *       │                        ├─┐
 *       │                        ┌─┘
 *       │                        │
 *       └─┐  ┐  ┌───────┬──┐  ┌──┘  + + + +
 *         │ ─┤ ─┤       │ ─┤ ─┤
 *         └──┴──┘       └──┴──┘  + + + +
 *                神兽保佑
 *               代码无BUG!
 */
/**
 * 权限控制器
 * @author ld
 *
 */
@Controller
@RequestMapping("/system/power")
public class PowerController {
	
	/** 关联业务逻辑层 */
	@javax.annotation.Resource
	private PowerService powerService;
	
	/**
	 * 查询用户拥有的角色ID
	 * @param userId 用户ID
	 * @return 角色ID集合
	 */
	@RequestMapping(value = "/userHasRoleId", method = RequestMethod.GET)
	@ResponseBody
	public List<String> queryUserHasRoleId(String userId) {
		//调用用户拥有角色资源
		List<String> roleList = powerService.queryUserHasRoleId(userId);
		return roleList;
	}
	
	/**
	 * 查询角色拥有的资源ID
	 * @param roleId 角色ID
	 * @return 资源ID集合
	 */
	@RequestMapping(value = "/roleHasResourceId", method = RequestMethod.GET)
	@ResponseBody
	public List<String> queryRoleHasResourceId(String roleId) {
		//调用查询角色拥有资源方法
		List<String> resourceIdList = powerService.queryRoleHasResourceId(roleId);
		return resourceIdList;
	}
	
	/**
	 * 查询用户带条件
	 * @param userVo 用户包装类
	 * @return 查询到的用户
	 */
	@RequestMapping(value = "/users", method = RequestMethod.GET)
	@ResponseBody
	public Map<String, Object> queryUser(UserVo userVo) {
		//调用查询用户方法
		Map<String, Object> resultMap = powerService.queryUser(userVo);
		return resultMap;
	}
	
	/**
	 * 查询角色带条件
	 * @param roelVo 角色包装类
	 * @return 查询到的角色
	 */
	@RequestMapping(value = "/roles", method = RequestMethod.GET)
	@ResponseBody
	public Map<String, Object> queryRole(RoleVo roelVo) {
		//调用查询角色方法
		Map<String, Object> resultMap = powerService.queryRole(roelVo);
		return resultMap;
	}
	
	/**
	 * 修改用户拥有角色
	 * @param userId 用户ID
	 * @param roleIds 角色ID数组
	 * @param flag 添加还是删除标志
	 * @return 成功返回true，失败false
	 */
	@RequestMapping(value = "/userRole", method = RequestMethod.PUT)
	@ResponseBody
	public Boolean updateUserRole(String userId, String[] roleIds, String flag) {
		//调用修改用户角色资源
		boolean result = powerService.updateUserRole(userId, roleIds, flag);
		return result;
	}
	
	/**
	 * 修改角色拥有资源
	 * @param roleId 角色ID
	 * @param resourceIds 资源ID数组
	 * @return 成功返回true，失败false
	 */
	@RequestMapping(value = "/roleResource", method = RequestMethod.PUT)
	@ResponseBody
	public Boolean updateRoleResource(String roleId, String[] resourceIds) {
		//调用修改角色资源方法
		boolean result = powerService.updateRoleResource(roleId, resourceIds);
		return result;
	}
	
	/**
	 * 查询所有角色
	 * @return 角色集合
	 */
	@RequestMapping(value = "/allRole", method = RequestMethod.GET)
	@ResponseBody
	public List<Role> queryAllRole() {
		//调用查询所有角色方法
		List<Role> roleList = powerService.queryAllRole();
		return roleList;
	}
	
	/**
	 * 查询所有资源
	 * @return 资源集合
	 */
	@RequestMapping(value = "/allResource", method = RequestMethod.GET)
	@ResponseBody
	public List<Resource> queryResource() {
		//调用查询所有资源方法
		List<Resource> resourceList = powerService.queryAllResource();
		return resourceList;
	}
	
	/**
	 * 查询用户拥有的资源
	 * @param request http请求对象
	 * @return 资源集合
	 */
	@RequestMapping(value = "/userHasResource", method = RequestMethod.POST)
	@ResponseBody
	public List<Resource> queryUserHasResource(HttpServletRequest request) {
		//获得session对象
		HttpSession session = request.getSession();
		//获得session中的用户对象
		User user = (User) session.getAttribute("user");
		//获取用户ID
		String userId = user.getUserId();
		//调用用户拥有资源方法
		List<Resource> resourceList = powerService.queryUserHasResource(userId);
		return resourceList;
	}
	
	/**
	 * 查询指定资源页面用户拥有的资源按钮
	 * @param roelVo
	 * @return
	 */
	@RequestMapping(value = "/userHasResourceBtn", method = RequestMethod.POST)
	@ResponseBody
	public List<String> userHasResourceBtn(HttpServletRequest request, String resourceName) {
		//获得session对象
		HttpSession session = request.getSession();
		//获得session中的用户对象
		User user = (User) session.getAttribute("user");
		//获取用户ID
		String userId = user.getUserId();
		//查询用户拥有的资源按钮
		List<String> resourceNameList = powerService.queryUserHasResourceBtn(userId, resourceName);
		return resourceNameList;
	}
}